/**
 * hnjz.com Inc.
 * Copyright (c) 2004-2013 All Rights Reserved.
 */
package com.hnjz.sys.role;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hnjz.common.AppException;
import com.hnjz.common.FyWebResult;
import com.hnjz.common.PKUtil;
import com.hnjz.common.YnEnum;
import com.hnjz.common.dao.domain.FyCounter;
import com.hnjz.common.domain.Combobox;
import com.hnjz.common.security.CtxUtil;
import com.hnjz.common.security.OperateUtil;
import com.hnjz.common.util.LogUtil;

/**
 * 角色管理
 * 
 * @author lijingfen
 * @version $Id: RoleManager.java $
 */
@Service("roleManager")
public class RoleManager {
    /**日志*/
    private static final Log log = LogFactory.getLog(RoleManager.class);

    @Autowired
    private RoleMapper       roleMapper;

    /**
     * 根据Id获取
     * 
     * @param id 角色ID
     * @return 角色
     */
    public Role getRole(String id) {
        if (StringUtils.isBlank(id)) {
            return null;
        }
        Role role = roleMapper.findById(id);
        return role;
    }

    /**
     * 查询出所有的角色，供下拉框使用
     * 
     * @return 所有的角色列表
     */
    public List<Combobox> queryAll() {
        List<Combobox> re = new ArrayList<Combobox>();
        List<Role> roles = this.roleMapper.findList();
        Combobox c = null;
        for (Role ele : roles) {
            c = new Combobox(ele.getId(), ele.getJsname());
            re.add(c);
        }
        return re;
    }

    /**
     * 查询角色列表
     * 
     * @param name 搜索条件，可以按名称，备注搜索
     * @param page 第几页
     * @param pageSize 每页显示多少条
     * @return 功能菜单列表
     * @throws Exception 
     */
    public FyWebResult queryRole(String name, String page, String pageSize) throws Exception {
        Role role = new Role();
        role.setJsname(name);
        long count = this.roleMapper.selectCountByPage(role);
        FyCounter c = new FyCounter();
        c.setNum(count);
        c.setCurrentPage(Integer.parseInt(page));
        c.setPerPageNum(pageSize);
        c.execute();
        if (log.isDebugEnabled()) {
            log.debug(c.getStartItem() + "：" + c.getPerPageNum());
        }
        RowBounds rbs = new RowBounds(c.getStartItem(), c.getPerPageNum());
        FyWebResult fy = new FyWebResult(c);
        List<Role> roles = this.roleMapper.findListByPage(role, rbs);
        List<Map<String, String>> rows = new ArrayList<Map<String, String>>();
        Map<String, String> dataObject = null;
        for (Role ele : roles) {
            dataObject = new HashMap<String, String>();
            dataObject.put("id", ele.getId());
            dataObject.put("code", ele.getCode());
            dataObject.put("jsname", ele.getJsname());
            dataObject.put("description", ele.getDescription());
            dataObject.put("operate", OperateUtil.getOperate(ele.getId()));
            rows.add(dataObject);
        }
        fy.setRows(rows);
        if (log.isDebugEnabled()) {
            log.debug("查询结果：" + LogUtil.m(fy));
        }
        return fy;
    }

    /**
     * 保存角色
     * 
     * @param frm {@link RoleForm}
     */
    public void saveRole(RoleForm frm) throws AppException {

        Role po = null;
        Date cur = new Date();
        if (StringUtils.isNotBlank(frm.getId())) {
            po = this.roleMapper.findById(frm.getId());
        } else {
            po = new Role();
            po.setId(PKUtil.uuid());
            po.setCreateTime(cur);
            po.setCreator(CtxUtil.getCurUser().getId());
            po.setVersion(1);
        }
        po.setJsname(frm.getJsname());
        po.setDescription(frm.getDescription());
        po.setIsActive(YnEnum.Y.getCode());
        po.setIssys(YnEnum.Y.getCode());

        if (StringUtils.isNotBlank(frm.getId())) {
            this.roleMapper.update(po);
        } else {
            this.roleMapper.save(po);
        }
    }

    /**
     * 删除角色
     * 
     * @param id 用户ID
     */
    public void removeRole(String id) {
        Role role = new Role();
        role.setId(id);
        this.roleMapper.deleteById(role);

    }
}
